package cn.iocoder.yudao.module.resource.service.usertimetransaction;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.resource.api.enums.EnumAipError;
import cn.iocoder.yudao.module.resource.api.enums.UserTimeTransactionTypeEnum;
import cn.iocoder.yudao.module.resource.controller.admin.usertimetransaction.vo.UserTimeTransactionPageReqVO;
import cn.iocoder.yudao.module.resource.controller.admin.usertimetransaction.vo.UserTimeTransactionSaveReqVO;
import cn.iocoder.yudao.module.resource.dal.dataobject.usertimetransaction.UserTimeTransactionDO;
import cn.iocoder.yudao.module.resource.dal.mysql.usertimetransaction.UserTimeTransactionMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 用户时长交易记录 Service 实现类
 *
 * @author lizhongwen
 */
@Service
@Validated
public class UserTimeTransactionServiceImpl implements UserTimeTransactionService {

    @Resource
    private UserTimeTransactionMapper userTimeTransactionMapper;

    @Override
    public String createUserTimeTransaction(UserTimeTransactionSaveReqVO createReqVO) {
        // 插入
        UserTimeTransactionDO userTimeTransaction = BeanUtils.toBean(createReqVO, UserTimeTransactionDO.class);
        userTimeTransactionMapper.insert(userTimeTransaction);
        // 返回
        return userTimeTransaction.getId();
    }

    @Override
    public void updateUserTimeTransaction(UserTimeTransactionSaveReqVO updateReqVO) {
        // 校验存在
        validateUserTimeTransactionExists(updateReqVO.getId());
        // 更新
        UserTimeTransactionDO updateObj = BeanUtils.toBean(updateReqVO, UserTimeTransactionDO.class);
        userTimeTransactionMapper.updateById(updateObj);
    }

    @Override
    public void deleteUserTimeTransaction(String id) {
        // 校验存在
        validateUserTimeTransactionExists(id);
        // 删除
        userTimeTransactionMapper.deleteById(id);
    }

    private void validateUserTimeTransactionExists(String id) {
        if (userTimeTransactionMapper.selectById(id) == null) {
            throw exception(EnumAipError.USER_TIME_TRANSACTION_NOT_EXISTS);
        }
    }

    @Override
    public UserTimeTransactionDO getUserTimeTransaction(String id) {
        return userTimeTransactionMapper.selectById(id);
    }

    @Override
    public UserTimeTransactionDO getUserTimeTransaction(Long userId, String id) {
        return userTimeTransactionMapper.selectOne(UserTimeTransactionDO::getUserId, userId, UserTimeTransactionDO::getId, id);
    }

    @Override
    public PageResult<UserTimeTransactionDO> getUserTimeTransactionPage(UserTimeTransactionPageReqVO pageReqVO) {
        return userTimeTransactionMapper.selectPage(pageReqVO);
    }

    @Override
    public UserTimeTransactionDO getUserTimeTransactionByGameMatchId(String gameMatchId) {
        return userTimeTransactionMapper.selectOne(UserTimeTransactionDO::getGameMatchId, gameMatchId);
    }

    @Override
    public List<UserTimeTransactionDO> getUserTimeTransactionByType(Long userId, UserTimeTransactionTypeEnum transactionType) {
        return userTimeTransactionMapper.selectList(UserTimeTransactionDO::getUserId, userId, UserTimeTransactionDO::getTransactionType, transactionType.getValue());
    }

    @Override
    public Boolean hasUserTimeTransactionByType(Long userId, UserTimeTransactionTypeEnum transactionType) {
        final LambdaQueryWrapper<UserTimeTransactionDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(UserTimeTransactionDO::getUserId, userId)
                .eq(UserTimeTransactionDO::getTransactionType, transactionType.getValue());
        final boolean exists = userTimeTransactionMapper.exists(wrapper);
        return exists;
    }
}